<?php

namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\BidBookModel;
use app\index\model\BidCustomerModel;
use app\index\model\BidGrantBookModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\BidSumListModel;
use app\index\model\BidSumModel;
use app\index\model\BidTaskGeneralModel;
use app\index\model\BidTaskModel;
use app\index\model\BidTaskTemplateModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\OptionModel;
use app\index\model\QianBanModel;
use app\index\model\QianPiaoModel;
use app\index\model\QianShouListModel;
use app\index\model\QianShouModel;
use app\index\model\QianZhiListModel;
use app\index\model\QianZhiModel;
use app\index\model\TouFanModel;
use app\index\model\TouJiaoModel;
use app\index\model\UserModel;
use app\index\relevan\CertRelevan;
use app\index\service\BidService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\QianService;
use app\index\service\UserService;
use app\index\service\WorkerService;
use app\index\validate\BidSum;
use app\index\validate\CertUse;
use think\Db;
use think\Request;

class Bid extends Common
{
    public function list_bid(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','in',[0,1]];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['bid.tou_unit','=', $param['tou_unit']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['bid.jinglei','=', $param['jinglei']];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['bid.status','=', $param['status']];
        }


        $data['tou_unit'] =(new UserService())->get_chaninfo_all();
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $jinglei = '';
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $jinglei = $param['jinglei'];
        }

        $missName = $jinglei == '直营' ? 'bid' : 'bid_ban';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where($missName, 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['state_name'] = (new BidService())->get_bid_state_name_wen($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth($missName);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['jinglei'])){
                return outputJson(4000, '请选择经营类别');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入工程名称');
            }
            if((new BidService())->check_bid_name($param['name'], $param['id'])){
                return outputJson(4000, '工程名称已存在');
            }
            if(empty($param['jiaer'])){
                return outputJson(4000, '请输入甲方名称');
            }
            if(empty($param['tou_unit'])){
                return outputJson(4000, '请选择投标单位');
            }
            if(empty($param['gui_unit'])){
                return outputJson(4000, '请选择业务归属');
            }
            if(empty($param['lanshi'])){
                return outputJson(4000, '请选择承揽方式');
            }
            if(empty($param['kaitype'])){
                return outputJson(4000, '请选择开标类型');
            }
            if(empty($param['kailei'])){
                return outputJson(4000, '请选择工程性质');
            }
            if(empty($param['zhaotype'])){
                return outputJson(4000, '请选择招标方式');
            }
            if(empty($param['islu'])){
                return outputJson(4000, '请选择业绩是否录入平台');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                BidModel::update($param);

                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid', $param['id'], '工程报备');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['tou_unit_info'] = (new UserService())->get_users_part_arr($data['data']['tou_unit'], 'dept');
            $data['data']['gui_unit_info'] = (new UserService())->get_users_part_arr($data['data']['gui_unit'], 'dept');
            $data['data']['jinger_info'] = (new UserService())->get_users_part_arr($data['data']['jinger'], 'user');
            $data['data']['touer_info'] = (new UserService())->get_users_part_arr($data['data']['touer'], 'user');


            $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
            $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
            $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
            $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
            $data['areas'] = (new OptService())->get_city_cader();


            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_bid(Request $request){
        $param = $request->post();
        $data = BidModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            BidModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bid', $data['id'], '投标项目');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_bid(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BidModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['state_name'] = (new BidService())->get_bid_state_name_tag($data['data']);
        $data['data']['state_name_wen'] = (new BidService())->get_bid_state_name_wen($data['data']);
        $data['data']['tou_unit_name'] = DeptModel::where(['id'=>$data['data']['tou_unit']])->value('name');
        $data['data']['gui_unit_name'] = DeptModel::where(['id'=>$data['data']['gui_unit']])->value('name');
        $data['data']['jinger_name'] = UserModel::get_realname_by_pk($data['data']['jinger']);
        $data['data']['touer_name'] = UserModel::get_realname_by_pk($data['data']['touer']);

        $data['data']['chanid_name'] = DeptModel::get_name_by_pk($data['data']['chanid']);

        $data['data']['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $data['data']['lanshi']);
        $data['data']['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $data['data']['zilai']);
        $data['data']['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $data['data']['kaitype']);
        $data['data']['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $data['data']['kailei']);
        $data['data']['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $data['data']['zhaotype']);

        $data['data']['state_name'] = (new BidService())->get_bid_state_name_tag($data['data']);
        $data['data']['state_name_wen'] = (new BidService())->get_bid_state_name_wen($data['data']);
        $data['data']['created_user_name'] = UserModel::get_realname_by_pk($data['data']['created_user']);

        $data['bookinfo'] = BidBookModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->select()->toArray();

        $data['ren_xiang_info'] = [];
        $data['data']['ren_xiang_str'] = "";
        $ren_xiang_info = CertModel::where('id','in',$data['data']['ren_xiang'])->select()->toArray();
        foreach ($ren_xiang_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_xiang_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            if($data['data']['ren_xiang_str']){
                $data['data']['ren_xiang_str'] .= " | ".$personname.'('.$v['name'].')';
            }else{
                $data['data']['ren_xiang_str'] .= $personname.'('.$v['name'].')';
            }
        }

        $data['ren_ji_info'] = [];
        $data['data']['ren_ji_str'] = "";
        $ren_ji_info = CertModel::where('id','in',$data['data']['ren_ji'])->select()->toArray();
        foreach ($ren_ji_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_ji_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            if($data['data']['ren_ji_str']){
                $data['data']['ren_ji_str'] .= " | ".$personname.'('.$v['name'].')';
            }else{
                $data['data']['ren_ji_str'] .= $personname.'('.$v['name'].')';
            }
        }

        $data['ren_zhi_info'] = [];
        $data['data']['ren_zhi_str'] = "";
        $ren_zhi_info = CertModel::where('id','in',$data['data']['ren_zhi'])->select()->toArray();
        foreach ($ren_zhi_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_zhi_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            if($data['data']['ren_zhi_str']){
                $data['data']['ren_zhi_str'] .= " | ".$personname.'('.$v['name'].')';
            }else{
                $data['data']['ren_zhi_str'] .= $personname.'('.$v['name'].')';
            }
        }

        $data['ren_an_info'] = [];
        $data['data']['ren_an_str'] = "";
        $ren_an_info = CertModel::where('id','in',$data['data']['ren_an'])->select()->toArray();
        foreach ($ren_an_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_an_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            if($data['data']['ren_an_str']){
                $data['data']['ren_an_str'] .= " | ".$personname.'('.$v['name'].')';
            }else{
                $data['data']['ren_an_str'] .= $personname.'('.$v['name'].')';
            }
        }

        $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function xiang_bid(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BidModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['qian_count'] = (new QianService())->get_bid_qian_count($data['data']['id']);

        $data['data']['state_name'] = (new BidService())->get_bid_state_name_tag($data['data']);
        $data['data']['state_name_wen'] = (new BidService())->get_bid_state_name_wen($data['data']);
        $data['data']['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $data['data']['lanshi']);
        $data['data']['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $data['data']['kailei']);
        $data['data']['gui_unit_name'] = DeptModel::where(['id'=>$data['data']['gui_unit']])->value('name');

        $data['bookinfo'] = BidBookModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->order('id DESC')->find();

        $data['ren_xiang_info'] = [];
        $ren_xiang_info = CertModel::where('id','in',$data['data']['ren_xiang'])->select()->toArray();
        foreach ($ren_xiang_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_xiang_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
        }

        $data['ren_ji_info'] = [];
        $ren_ji_info = CertModel::where('id','in',$data['data']['ren_ji'])->select()->toArray();
        foreach ($ren_ji_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_ji_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
        }

        $data['ren_zhi_info'] = [];
        $ren_zhi_info = CertModel::where('id','in',$data['data']['ren_zhi'])->select()->toArray();
        foreach ($ren_zhi_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_zhi_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
        }

        $data['ren_an_info'] = [];
        $ren_an_info = CertModel::where('id','in',$data['data']['ren_an'])->select()->toArray();
        foreach ($ren_an_info as $v){
            $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
            $data['ren_an_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
        }



        $data['tou_zhuang'] = 0;
        $yitui = QianZhiListModel::alias('list')
            ->join('qian_zhi qian','qian.id=list.zhiid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['qian.bidid'=>$data['data']['id'], 'list.zhiqianlei'=>5])->find();
        if($yitui){
            $data['tou_zhuang'] = 4;
        }else{
            $yifan = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->find();
            if($yifan){
                $data['tou_zhuang'] = 3;
            }else{
                $yijiao = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->find();
                if($yijiao){
                    $data['tou_zhuang'] = 2;
                }else{
                    $yishou = QianShouListModel::alias('list')
                        ->join('qian_shou qian','qian.id=list.shouid')
                        ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
                        ->where(['qian.bidid'=>$data['data']['id'], 'list.shouqianlei'=>1])->find();
                    if($yishou){
                        $data['tou_zhuang'] = 1;
                    }
                }
            }
        }

        $data['shou_bao_fee'] = QianShouListModel::alias('list')
            ->join('qian_shou qian','qian.id=list.shouid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['qian.bidid'=>$data['data']['id'], 'list.shouqianlei'=>1])
            ->sum('list.money');
        $data['tou_jiao_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->sum('money');
        $data['tou_fan_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->sum('money');
        $data['tui_bao_fee'] = QianZhiListModel::alias('list')
            ->join('qian_zhi qian','qian.id=list.zhiid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['qian.bidid'=>$data['data']['id'], 'list.zhiqianlei'=>5])
            ->sum('list.money');
        $data['ying_dian_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->sum('dianfee');
        $data['shou_dianli_fee'] = QianShouListModel::alias('list')
            ->join('qian_shou qian','qian.id=list.shouid')
            ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
            ->where(['qian.bidid'=>$data['data']['id'], 'list.shouqianlei'=>4])
            ->sum('list.money');
        $data['daishou_dianli_fee'] = round($data['ying_dian_fee'] - $data['shou_dianli_fee'], 2);
        $data['daishou_dianli_fee'] = $data['daishou_dianli_fee'] > 0 ? $data['daishou_dianli_fee'] : 0;

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function tou_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                BidModel::update($param);

                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('投标', 'bid', $param['id']);
                (new OptService())->add_log_list('投标', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $bookinfo = BidBookModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'bidid'=>$data['data']['id']])->column('name');
            $data['data']['bookinfo'] = implode('，',$bookinfo);

            $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function kai_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            if($param['state'] == 6){
                $param['windt'] = '';
                $param['winfee'] = '';
//                if($param['islu'] == '否'){
//                    $param['ren_xiang'] = '';
//                    $param['ren_ji'] = '';
//                    $param['ren_zhi'] = '';
//                    $param['ren_an'] = '';
//                }
            }

            Db::startTrans();
            try {
                BidModel::update($param);

                (new CertRelevan())->set_cert_statueuse_bid($param['id']);

                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('开标', 'bid', $param['id']);
                (new OptService())->add_log_list('开标', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['ren_xiang_info'] = [];
            $ren_xiang_info = CertModel::where('id','in',$data['data']['ren_xiang'])->select()->toArray();
            foreach ($ren_xiang_info as $v){
                $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
                $data['ren_xiang_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            }

            $data['ren_ji_info'] = [];
            $ren_ji_info = CertModel::where('id','in',$data['data']['ren_ji'])->select()->toArray();
            foreach ($ren_ji_info as $v){
                $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
                $data['ren_ji_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            }

            $data['ren_zhi_info'] = [];
            $ren_zhi_info = CertModel::where('id','in',$data['data']['ren_zhi'])->select()->toArray();
            foreach ($ren_zhi_info as $v){
                $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
                $data['ren_zhi_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            }

            $data['ren_an_info'] = [];
            $ren_an_info = CertModel::where('id','in',$data['data']['ren_an'])->select()->toArray();
            foreach ($ren_an_info as $v){
                $personname = CertPersonModel::where(['id'=>$v['personid']])->value('name');
                $data['ren_an_info'][] = ['id'=>$v['id'],'name'=>$personname.'('.$v['name'].')'];
            }


            $data['kai_guo'] = (new BidService())->get_bid_kai_guo();
            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function list_bid_jindu_lan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['bid.jinglei','=', $param['jinglei']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['tou_unit.name','like', '%'.$param['tou_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['bid.status','=', $param['status']];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['bid.gui_unit','=', $param['gui_unit']];
        }


        $data['tou_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';
        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);


                $ziqu = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->value('ziqu');

                $item['ziqu_name'] = (new QianService())->get_tou_jiao_ziqu_name($ziqu);
                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);


                $item['state_name'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['tou_jiao_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');

                $item['isbook'] = false;
                $bookinfo = BidBookModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'bidid'=>$item['id']])->find();
                if($bookinfo){
                    $item['isbook'] = true;
                }


                $item['tou_zhuang'] = 0;
                $yitui = QianZhiListModel::alias('list')
                    ->join('qian_zhi qian','qian.id=list.zhiid')
                    ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
                    ->where(['qian.bidid'=>$item['id'], 'list.zhiqianlei'=>5])->find();
                if($yitui){
                    $item['tou_zhuang'] = 4;
                }else{
                    $yifan = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                        ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->find();
                    if($yifan){
                        $item['tou_zhuang'] = 3;
                    }else{
                        $yijiao = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                            ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->find();
                        if($yijiao){
                            $item['tou_zhuang'] = 2;
                        }else{
                            $yishou = QianShouListModel::alias('list')
                                ->join('qian_shou qian','qian.id=list.shouid')
                                ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
                                ->where(['qian.bidid'=>$item['id'], 'list.shouqianlei'=>1])->find();
                            if($yishou){
                                $item['tou_zhuang'] = 1;
                            }
                        }
                    }
                }

                $item['tou_jiao_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['tou_fan_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_bid_win_lan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];
        $where[] = ['bid.state','=',3];

        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['bid.jinglei','=', $param['jinglei']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['tou_unit.name','like', '%'.$param['tou_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['bid.gui_unit','=', $param['gui_unit']];
        }


        $data['tou_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';
        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';


        if(isset($param['islan']) && $param['islan'] != ''){
            if($param['islan'] == 1){
                $where[] = ['e.id','>', 0];
            }elseif($param['islan'] == 2){
                $where[] = ['e.id','=', null];
            }
        }

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name,e.id as enginid')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->leftJoin('engin e','e.bidid=bid.id and e.deleted_user=0 and e.isturn=1 and e.status in (0,1)')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['state_name'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

//                $item['enginid'] = 0;
//                $exist_engin = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'bidid'=>$item['id']])->where('status','in',[0,1])->find();
//                if($exist_engin){
//                    $item['enginid'] = $exist_engin['id'];
//                }



                return $item;
            })->toArray();


        $data['butauth'] = (new MenuService())->get_butauth('bid_win_lan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_lan(Request $request){
        $param = $request->post();

        $ke_shou_id = OptionModel::get_pk_by_num($this->userinfo['comid'], 'cai_shouqianlei');
        $ke_shou_list = OptionModel::field('id,keyid as value,name as title,name')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$ke_shou_id])
            ->order('sort ASC')->select()->toArray();

        $ke_zhi_id = OptionModel::get_pk_by_num($this->userinfo['comid'], 'cai_zhiqianlei');
        $ke_zhi_list = OptionModel::field('id,keyid as value,name as title,name')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$ke_zhi_id])
            ->order('sort ASC')->select()->toArray();

        if(isset($param['seatype']) && $param['seatype'] == 'col'){
            $data['ke_shou_list'] = $ke_shou_list;
            $data['ke_zhi_list'] = $ke_zhi_list;
            return outputJson(2000,lang('code_2000'),$data);
        }


        get_page($param);
        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];
        $where[] = ['bid.jinglei','=','总公司'];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['bid.id','=', $param['bidid']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['tou_unit.name','like', '%'.$param['tou_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['bid.status','=', $param['status']];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['bid.gui_unit','=', $param['gui_unit']];
        }


        $data['tou_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';
        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';
        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($ke_shou_list,$ke_zhi_list){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['qian_shou_fee'] = QianShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');

                $item['qian_zhi_fee'] = QianZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['qian_zhi_fee'] += TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('koudaifee');

                $item['ying_dian_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id'],'ziqu'=>2])->sum('dianfee');

                $item['tou_jiao'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');

                $item['tou_fan_11'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['tou_fan_22'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('koudaifee');

                $item['tou_fan'] = round($item['tou_fan_11'] + $item['tou_fan_22'], 2);

                $item['qian_run_fee'] = round($item['qian_shou_fee'] - $item['qian_zhi_fee'] - $item['tou_jiao'] + $item['tou_fan'], 2);


                $shouheji = 0;
                foreach ($ke_shou_list as $vvShou){
                    $shousonArr = (new OptService())->get_option_ids($vvShou['id']);
                    $shousonArr = array_merge($shousonArr,[$vvShou['value']]);

                    $fee =  QianShouListModel::alias('list')
                        ->join('qian_shou qian','qian.id=list.shouid')
                        ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
                        ->where(['qian.bidid'=>$item['id']])
                        ->where('list.shouqianlei','in',$shousonArr)
                        ->sum('list.money');
                    $item['shou_'.$vvShou['value']] = $fee;
                    $shouheji += $fee;
                }
                $item['shou_he'] = round($shouheji, 2);

                $item['daifu_kou_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('koudaifee');

                $zhiheji = $item['daifu_kou_fee'];
                foreach ($ke_zhi_list as $vvZhi){
                    $zhisonArr = (new OptService())->get_option_ids($vvZhi['id']);
                    $zhisonArr = array_merge($zhisonArr,[$vvZhi['value']]);

                    $fee =  QianZhiListModel::alias('list')
                        ->join('qian_zhi qian','qian.id=list.zhiid')
                        ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
                        ->where(['qian.bidid'=>$item['id']])
                        ->where('list.zhiqianlei','in',$zhisonArr)
                        ->sum('list.money');
                    $item['zhi_'.$vvZhi['value']] = $fee;
                    $zhiheji += $fee;
                }
                $item['zhi_he'] = round($zhiheji, 2);


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_ban_lan(Request $request){
        $param = $request->post();

        $ke_zhi_id = OptionModel::get_pk_by_num($this->userinfo['comid'], 'cai_zhiqianlei');
        $ke_zhi_list = OptionModel::field('id,keyid as value,name as title,name')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$ke_zhi_id])
            ->order('sort ASC')->select()->toArray();

        if(isset($param['seatype']) && $param['seatype'] == 'col'){
            $data['ke_zhi_list'] = $ke_zhi_list;
            return outputJson(2000,lang('code_2000'),$data);
        }


        get_page($param);
        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];
        $where[] = ['bid.jinglei','=','分公司(办事处)'];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['bid.id','=', $param['bidid']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['tou_unit.name','like', '%'.$param['tou_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['bid.status','=', $param['status']];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['bid.gui_unit','=', $param['gui_unit']];
        }


        $data['tou_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';
        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';
        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($ke_zhi_list){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);


                $item['qian_zhi_fee'] = QianZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['qian_zhi_fee'] += TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('koudaifee');

                $item['ying_dian_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id'],'ziqu'=>2])->sum('dianfee');

                $item['tou_jiao'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');

                $item['tou_fan_11'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['tou_fan_22'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('koudaifee');

                $item['tou_fan'] = round($item['tou_fan_11'] + $item['tou_fan_22'], 2);


                $item['daifu_kou_fee'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'bidid'=>$item['id']])->sum('koudaifee');

                $zhiheji = $item['daifu_kou_fee'];
                foreach ($ke_zhi_list as $vvZhi){
                    $zhisonArr = (new OptService())->get_option_ids($vvZhi['id']);
                    $zhisonArr = array_merge($zhisonArr,[$vvZhi['value']]);

                    $fee =  QianZhiListModel::alias('list')
                        ->join('qian_zhi qian','qian.id=list.zhiid')
                        ->where(['qian.comid'=>$this->userinfo['comid'],'qian.deleted_user'=>0,'qian.isturn'=>1,'qian.status'=>1])
                        ->where(['qian.bidid'=>$item['id']])
                        ->where('list.zhiqianlei','in',$zhisonArr)
                        ->sum('list.money');
                    $item['zhi_'.$vvZhi['value']] = $fee;
                    $zhiheji += $fee;
                }
                $item['zhi_he'] = round($zhiheji, 2);


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_qian_gofuer_lan(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];
        $where[] = ['bid.jinglei','=','分公司(办事处)'];

        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }

        $data['data'] = BidModel::alias('bid')
            ->field('bid.gofuer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.gofuer DESC')
            ->group('bid.gofuer')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['qian_shou_fee'] = QianBanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'gofuer'=>$item['gofuer']])->sum('money');

                $idArr = BidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'jinglei'=>'分公司(办事处)','gofuer'=>$item['gofuer']])->column('id');
                $item['nums'] = count($idArr);

                $item['qian_zhi_fee'] = QianZhiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,])->where('bidid','in',$idArr)->sum('money');
                $item['qian_zhi_fee'] += TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('koudaifee');

                $item['tou_jiao_fee'] = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->where('bidid','in',$idArr)->sum('money');

                $jiaoIdArr = TouJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'ziqu'=>1])->where('bidid','in',$idArr)->column('id');

                $item['tou_fan_11'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->where('jiaoid','in',$jiaoIdArr)->sum('money');
                $item['tou_fan_22'] = TouFanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->where('jiaoid','in',$jiaoIdArr)->sum('koudaifee');

                $item['tou_fan_fee'] = round($item['tou_fan_11'] + $item['tou_fan_22'], 2);
                $item['qian_yu_fee'] = round($item['qian_shou_fee'] - $item['qian_zhi_fee'] - $item['tou_jiao_fee'] + $item['tou_fan_fee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    /************************************ 标书 ************************************/
    public function list_bid_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=','0'];
        $where[] = ['book.comid','=',$this->userinfo['comid']];
        $where[] = ['book.isturn','=',1];
        $where[] = ['book.status','in',[0,1]];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['book.bidid','=', $param['bidid']];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|book.name|b.number|b.gofuer|book.flowuser|u.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['maker']) && !empty($param['maker'])){
            $where[] = ['u.realname','like', '%'.$param['maker'].'%'];
        }
        if(isset($param['bid_number']) && !empty($param['bid_number'])){
            $where[] = ['b.number','like', '%'.$param['bid_number'].'%'];
        }
        if(isset($param['bid_state']) && !empty($param['bid_state'])){
            $where[] = ['b.state','=', $param['bid_state']];
        }
        if(isset($param['bid_gofuer']) && !empty($param['bid_gofuer'])){
            $where[] = ['b.gofuer','like', '%'.$param['bid_gofuer'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.id','=', $param['gui_unit']];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['book.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['book.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['book.status','=', $param['status']];
        }

        $data['bid_state'] = (new BidService())->get_bid_state();
        $data['is_bid_state'] = isset($param['bid_state']) ? $param['bid_state'] : '';

        $data['gui_unit'] = (new UserService())->get_chaninfo_all();
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';



        $data['data'] = BidBookModel::alias('book')->field('book.*,b.name as bid_name,b.jinglei
            ,b.enddt_tou,u.realname as maker_name
            ,gui_unit.name as gui_unit_name')
            ->leftJoin('bid b','b.id = book.bidid')
            ->leftJoin('dept gui_unit','gui_unit.id=b.gui_unit')
            ->leftJoin('user u','u.id = book.maker')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_book', 'book'))
            ->order('book.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bill_dt'] = '';
                $billinfo = FlowBillModel::where('id',$item['billid'])->find();
                if($billinfo){
                    $item['bill_dt'] = $billinfo['updated_at'];
                }

                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['created_user'] = UserModel::get_realname_by_pk($item['created_user']);

                return $item;
            })->toArray();

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['butauth'] = (new MenuService())->get_butauth('bid_book');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_bid_book(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidBookModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $bid = BidModel::where('id',$data['bidid'])->find();
        $data['bid_name'] = $bid['name'];
        $data['tou_unit_name'] = DeptModel::get_name_by_pk($bid['tou_unit']);

        $data['enddt_tou'] = $bid['enddt_tou'];

        $data['maker_name'] = UserModel::get_realname_by_pk($data['maker']);
        $data['chan_name'] = DeptModel::get_name_by_pk($data['chanid']);

        $data['files'] = (new OptService())->get_file('bid_book', $data['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['billid']);

        return outputJson(2000,lang('code_2000'), $data);
    }
    public function count_bid_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=','0'];
        $where[] = ['book.comid','=',$this->userinfo['comid']];
        $where[] = ['book.isturn','=',1];

        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['b.jinglei','=', $param['jinglei']];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['maker']) && !empty($param['maker'])){
            $where[] = ['u.realname','like', '%'.$param['maker'].'%'];
        }

        $makerIds = BidBookModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])->column('maker');
        $where[] = ['u.id','in',$makerIds];


        $data['data'] = UserModel::alias('u')
            ->field('book.*,b.name as bid_name,b.enddt_tou,b.state,b.kaidt,u.realname as maker_name,b.tou_unit,b.jinglei')
            ->leftJoin('bid_book book','u.id = book.maker')
            ->leftJoin('bid b','b.id = book.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_book_count', 'book'))
            ->order('book.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bill_dt'] = '';
                $billinfo = FlowBillModel::where('id',$item['billid'])->find();
                if($billinfo){
                    $item['bill_dt'] = $billinfo['updated_at'];
                }

                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['created_user'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['state'] = (new BidService())->get_bid_state_name_tag($item);
                $item['tou_unit'] = DeptModel::get_name_by_pk($item['tou_unit']);
                return $item;
            })->toArray();

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }



    /************************************ 授权委托书 ************************************/
    public function list_bid_grant_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=','0'];
        $where[] = ['book.comid','=',$this->userinfo['comid']];
        $where[] = ['book.isturn','=',1];

        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['u.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['book.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['book.dt','between',[$param['dt'].' 00:00:00',$param['dt'].' 23:59:59']];
        }




        $data['data'] = BidGrantBookModel::alias('book')->field('book.*,b.name as bid_name,b.number as bid_number,
            b.business,b.state,b.isqi,u.realname as created_user')
            ->leftJoin('bid b','b.id = book.bidid')
            ->leftJoin('user u','u.id = book.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_grant_book', 'book'))
            ->order('book.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bid_business'] = $item['business'] == 1 ? '自营' : '联营';
                $item['bid_state'] = (new BidService())->get_bid_state_name_tag($item);
                $item['name'] = str_replace('[]','，',$item['name']);
                $item['number'] = str_replace('[]','，',$item['number']);
                $item['dt'] = $item['startdt'].' - '.$item['enddt'];

                $item['bill_dt'] = '';
                $billinfo = FlowBillModel::where('id',$item['billid'])->find();
                if($billinfo){
                    $item['bill_dt'] = $billinfo['updated_at'];
                }
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['butauth'] = (new MenuService())->get_butauth('bid_grant_book');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function show_bid_grant_book(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidGrantBookModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $bid = BidModel::where('id',$data['bidid'])->find();
        $data['bid_name'] = $bid['name'];
        $data['bid_number'] = $bid['number'];

        $data['name'] = str_replace('[]','，',$data['name']);
        $data['number'] = str_replace('[]','，',$data['number']);
        $data['dt'] = $data['startdt'].' - '.$data['enddt'];

        $data['files'] = (new OptService())->get_file('bid_grant_book', $data['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




    /************************************ 投标保证金 ************************************/
    public function list_bid_bond(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['bid.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','eq',$param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','eq', $param['state']];
        }
        if(isset($param['bond_type']) && !empty($param['bond_type'])){
            $where[] = ['bid.bond_type','eq', $param['bond_type']];
        }




        $state = (new BidService())->get_bid_state();
        $data['data'] = BidModel::alias('bid')
            ->field('bid.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_bond', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                // 收取金额（元）,缴纳金额（元）,退回金额（元）,返还金额(元)
                $item['shou_money'] = EnginBondShouModel::where(['deleted_user'=>0,'bondtype'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['jiao_money'] = EnginBondJiaoModel::where(['deleted_user'=>0,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $jiaoIds = EnginBondJiaoModel::where(['deleted_user'=>0,'status'=>1,'bidid'=>$item['id']])->column('id');
                $item['tui_money'] = AccountListModel::where(['type'=>8])->where('mid','in',$jiaoIds)->sum('money');
                $item['fan_money'] = EnginBondFanModel::alias('fan')
                    ->leftJoin('engin_bond_shou shou','shou.id = fan.shouid')
                    ->where(['shou.deleted_user'=>0,'shou.status'=>1,'shou.bidid'=>$item['id'],
                        'fan.deleted_user'=>0,'fan.status'=>1])
                    ->sum('fan.money');
                return $item;
            })->toArray();

        $data['state'] = $state;
        $data['bond_type'] = BidModel::get_bond_type();
        $data['butauth'] = (new MenuService())->get_butauth('bid_bond');
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function edit_bid_bond(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $files = array_filter($param['files']);
            unset($param['files']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            Db::startTrans();
            try {
                BidModel::update($param);
                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('编辑保证金', 'bid', $param['id']);
                (new OptService())->add_log_list('编辑保证金', 'bid', $param['id'], '保证金');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
            if(!$result){
                return outputJson(4000,lang('code_4000'));
            }
            $data['data'] = $result;
            $data['bond_type'] = BidModel::get_bond_type();
            $data['files'] = (new OptService())->get_file('bid', $result['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    /************************************ 投标任务-通用模板 ************************************/
    public function list_bid_task_template(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];

        $data['data'] = BidTaskTemplateModel::alias('bid')
            ->field('bid.*')
            ->where($where)
            ->select()
            ->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    /************************************ 总结 ************************************/
    public function list_bid_sum(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['sum.deleted_user','=','0'];
        $where[] = ['sum.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['bid.jinglei','=', $param['jinglei']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['user.realname','like', '%'.$param['created_user_name'].'%'];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidSumModel::alias('sum')
            ->join('bid bid','bid.id = sum.bidid')
            ->join('user user','user.id = sum.created_user')
            ->field('sum.*,bid.name as bid_name,bid.state,bid.number,bid.kongfee,bid.jinglei
            ,bid.zhao_unit,bid.lanshi,bid.kaidt,bid.kailei
            ,bid.gui_unit,bid.gofuer,bid.status as bid_status')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_sum', 'sum'))
            ->order('sum.id desc')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new BidService())->get_bid_state_name_tag(['status'=>$item['bid_status'],'state'=>$item['state']]);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bid_status'],'state'=>$item['state']]);
                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['gui_unit_name'] = DeptModel::where(['id'=>$item['gui_unit']])->value('name');

                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid_sum');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function add_bid_sum(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $check = $this->validate($param,'BidSum.add');
            if($check !== true){
                return outputJson(4000,$check);
            }
            // 所有的明细处理成数组形式
            $list = array_filter($param['list']);
            unset($param['list']);

            $files = array_filter($param['files']);
            unset($param['files']);

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            Db::startTrans();
            try {
                $result = BidSumModel::create($param);

                foreach ($list as $item){
                    $item['mid'] = $result->id;
                    BidSumListModel::create($item);
                }

                (new OptService())->relation_file($files,'bid_sum',$result->id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_sum', $result->id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_sum', $result->id, '投标总结');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['files'] = (new OptService())->get_file('bid_sum');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }

    public function edit_bid_sum(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $check = $this->validate($param,'BidSum.edit');
            if($check !== true){
                return outputJson(4000,$check);
            }

            // 所有的明细处理成数组形式
            $list = array_filter($param['list']);
            unset($param['list']);

            $files = array_filter($param['files']);
            unset($param['files']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            Db::startTrans();
            try {
                BidSumModel::update($param);

                BidSumListModel::where('mid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['mid'] = $param['id'];
                    BidSumListModel::create($item);
                }

                (new OptService())->relation_file($files,'bid_sum',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid_sum', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid_sum', $param['id'], '投标总结');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidSumModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
            if(!$result){
                return outputJson(4000,lang('code_4000'));
            }

            $data['bidinfo'] = (new BidService())->get_bid_info($result['bidid']);

            $data['data'] = $result;

            $data['list'] = BidSumListModel::where('mid',$result['id'])->select()->toArray();
            $data['files'] = (new OptService())->get_file('bid_sum', $id);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function show_bid_sum(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidSumModel::where(['id'=>$id])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $data['bidinfo'] = (new BidService())->get_bid_info($data['bidid']);

        $data['list'] = BidSumListModel::where('mid',$data['id'])->select()->toArray();
        $data['files'] = (new OptService())->get_file('bid_sum', $data['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function del_bid_sum(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['id']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择需要删除的投标总结');
        }

        Db::startTrans();
        try {
            foreach ($ids as $id){
                $data = BidSumModel::where(['id'=>$id,'comid'=>$this->userinfo['comid']])->find();
                if(!$data){
                    continue;
                }
                BidSumModel::where(['id' => $data['id']])->update([
                    'deleted_user' => $this->userinfo['id'],
                    'deleted_at' => date('Y-m-d H:i:s')
                ]);
                (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bid_sum', $data['id'], '投标总结');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }






    public function bid_count_yeartou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $min_enddt_tou = BidModel::alias('bid')->where($where)
            ->where("bid.enddt_tou != '' and bid.enddt_tou is not null and bid.enddt_tou != '0000-00-00'")
            ->order('bid.enddt_tou ASC')->value('bid.enddt_tou');
        $max_enddt_tou = BidModel::alias('bid')->where($where)
            ->where("bid.enddt_tou != '' and bid.enddt_tou is not null and bid.enddt_tou != '0000-00-00'")
            ->order('bid.enddt_tou DESC')->value('bid.enddt_tou');
        if(!$min_enddt_tou){
            $data['data'] = [];
            return outputJson(2000,lang('code_2000'),$data);
        }
        $startYear = date('Y', strtotime($max_enddt_tou));
        $endYear = date('Y', strtotime($min_enddt_tou));

        $list = [];
        for ($i=(int)$startYear; $i>=$endYear; $i--){
            $bet1 = $i.'-01-01 00:00:00';
            $bet2 = $i.'-12-31 23:59:59';
            $zainum = BidModel::alias('bid')->where($where)->where(['state'=>1])->where('bid.enddt_tou','between',[$bet1, $bet2])->count();
            $winnum = BidModel::alias('bid')->where($where)->where(['state'=>3])->where('bid.enddt_tou','between',[$bet1, $bet2])->count();
            $luonum = BidModel::alias('bid')->where($where)->where(['state'=>6])->where('bid.enddt_tou','between',[$bet1, $bet2])->count();
            $list[] = [
                'year' => $i,
                'allnum' => $zainum+$winnum+$luonum,
                'zainum' => $zainum,
                'winnum' => $winnum,
                'luonum' => $luonum,
            ];
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function bid_count_yearkai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $min_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt ASC')->value('bid.kaidt');
        $max_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt DESC')->value('bid.kaidt');
        if(!$min_kaidt){
            $data['data'] = [];
            return outputJson(2000,lang('code_2000'),$data);
        }
        $startYear = date('Y', strtotime($max_kaidt));
        $endYear = date('Y', strtotime($min_kaidt));

        $list = [];
        for ($i=(int)$startYear; $i>=$endYear; $i--){
            $bet1 = $i.'-01-01 00:00:00';
            $bet2 = $i.'-12-31 23:59:59';
            $allnum = BidModel::alias('bid')->where($where)->where('state','in',[3,6])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $winnum = BidModel::alias('bid')->where($where)->where(['state'=>3])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $winfee = BidModel::alias('bid')->where($where)->where(['state'=>3])->where('bid.kaidt','between',[$bet1, $bet2])->sum('kai_fee');
            $winrate = 0;
            if($allnum != 0){
                $winrate = round($winnum/$allnum*100, 2);
            }

            $list[] = [
                'year' => $i,
                'allnum' => $allnum,
                'winnum' => $winnum,
                'winfee' => $winfee,
                'winrate' => $winrate,
            ];
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function bid_count_win(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $min_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt ASC')->value('bid.kaidt');
        $max_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt DESC')->value('bid.kaidt');
        if(!$min_kaidt){
            $data['data'] = [];
            return outputJson(2000,lang('code_2000'),$data);
        }
        $startYear = date('Y', strtotime($max_kaidt));
        $endYear = date('Y', strtotime($min_kaidt));

        $list = [];
        for ($i=(int)$startYear; $i>=$endYear; $i--){
            $bet1 = $i.'-01-01 00:00:00';
            $bet2 = $i.'-12-31 23:59:59';
            $zi_num = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>1])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $zi_fee = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>1])->where('bid.kaidt','between',[$bet1, $bet2])->sum('kai_fee');
            $lian_num = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>2])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $lian_fee = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>2])->where('bid.kaidt','between',[$bet1, $bet2])->sum('kai_fee');

            $list[] = [
                'year' => $i,
                'zi_num' => $zi_num,
                'zi_fee' => $zi_fee,
                'lian_num' => $lian_num,
                'lian_fee' => $lian_fee,
            ];
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }



}

















